package com.ima.web.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ima.web.beans.db.Categories;
import com.ima.web.beans.db.Users;
import com.ima.web.beans.form.CategoriesForm;
import com.ima.web.dao.CategoriesDao;
import com.ima.web.dao.UsersDao;
import com.ima.web.dao.impl.CategoriesDaoImpl;
import com.ima.web.dao.impl.UsersDaoImpl;
import com.ima.web.service.CategoriesService;
import com.ima.web.service.UserService;

@Service
@Transactional
public class CategoriesServiceImpl implements CategoriesService {

	private CategoriesDao categoriesDao;
	private UserService userService;
		
	@Autowired
	public void setDependencies(CategoriesDao categoriesDao,
							    UserService userService){
		this.categoriesDao = categoriesDao;
		this.userService = userService;
	}
	
	@Override
	public List<Categories> getForProcessing() {
		return categoriesDao.getToBeApproved();
	}

	@Override
	public List<Categories> getApprovedBy(Users user) {
		Categories cat = new Categories();
		cat.setApprover(user);
		
		return categoriesDao.list(cat);
	}

	@Override
	public List<Categories> getApprovedBy(int id) {
		Users u = userService.getUserData(id);//findById(id);
		Categories cat = new Categories();
		cat.setApprover(u);
		
		return categoriesDao.list(cat);
	}

	@Override
	public List<Categories> getApprovedBy(String username) {
		
		Users u = userService.getUserData(username);
		
		if(u!=null){
			System.out.println("getCategoriesApprovedBy(String)"+u.getUsername());
		}
		
		Categories c = new Categories();
		c.setApprover(u);
		c.setApproved(true);
		c.setDeleted(false);
		
		return categoriesDao.list(c);
	}

	@Override
	public void insert(Categories data) {
		categoriesDao.save(data);
		
	}

	@Override
	public Categories getById(int id) {
		return categoriesDao.findById(id);
	}

	@Override
	public Categories getByName(String category) {
		Categories c = new Categories();
		c.setName(category);
		
		List<Categories> cats =categoriesDao.list(c); 
		
		if(cats.size()>0)
			return cats.get(0);
		else
			return null;
		//TODO change nulls to exceptions
	}

	@Override
	public List<Categories> getList(Categories criteria) {

		return categoriesDao.list(criteria);
	}

	@Override
	public List<Categories> getApproved() {
		//System.out.println("Approved categories: "+categoriesDao.getApproved().size());
		Categories c = new Categories();
		c.setApproved(true);
		c.setDeleted(false);
		//return categoriesDao.getApproved();
		return categoriesDao.list(c);
	}

	@Override
	public void insert(CategoriesForm formData) {
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		//should always have username and should only have one
		Users u = userService.getUserData(username);
		
		Date now = new Date();
		
		Categories cat = new Categories();

		//No categories id, it's a new category
		if(formData.getCategoriesId() == null){
			cat.setCreatedDate(now);
			
			//set requestor, if its new - requester should be included
			cat.setRequester(u);
			cat.setRequestedDate(now);
		}else{
			cat = getById(formData.getCategoriesId());
		}
		
		cat.setCategoriesId(formData.getCategoriesId());
		cat.setName(formData.getName());
		cat.setDescription(formData.getDescription());
		
		//Only admin has direct approval
		if(formData.getApproved()){	
			cat.setApproved(true);
			cat.setApprovedDate(now);
			cat.setApprover(u);
		}else{
			cat.setApproved(false);
			cat.setApprover(null);
			cat.setApprovedDate(null);
		}
		
		cat.setUpdatedDate(now);
		
		
		categoriesDao.save(cat);
	}

	@Override
	public void approve(int categoriesId, boolean isApproved) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void approve(CategoriesForm formData) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void approve(Categories category, boolean isApproved) {
		// TODO Auto-generated method stub
		
	}

}
